﻿using DXBase;
using DXBase.D3D;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DXBase.Async;

namespace DXBase.D3D
{
	public static class Extensions
	{
		public async static Task SavePic(this DXContext ctxt, AsyncLock locker)
		{
			var tex = ctxt.Target.Snapshot();
			await SavePic(tex, locker);
		}
		public async static Task SavePic(this Texture2D tex, AsyncLock locker)
		{
			var picker = new Windows.Storage.Pickers.FileSavePicker()
			{
				FileTypeChoices =
				{
					{ "PNG File", new [] { ".png" } },
					{ "JPEG File", new [] { ".jpg", ".jpeg" } },
					{ "BMP File", new [] { ".bmp" } },
				},
				DefaultFileExtension = ".png",
				SuggestedFileName = "ImageCapture",
				SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary,
			};
			using (await locker.LockAsync())
			{
				var file = await picker.PickSaveFileAsync();
				if (file != null)
				{
					var fmt = DXBase.D3D.ImageFileFormat.PNG;
					switch (file.FileType)
					{
						case ".jpg":
						case ".jpeg":
							fmt = DXBase.D3D.ImageFileFormat.JPG;
							break;
						case ".bmp":
							fmt = DXBase.D3D.ImageFileFormat.BMP;
							break;
					}
					using (var f = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite))
						tex.Save(f, fmt);
				}
			}
		}
	}
}
